package cn.wolfcode.shop.message;

import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.vo.GenerateOrderVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;

/**
 * Created by wolfcode on 2018/8/21.
 */
@Component
public class OrderConsumer {
    @Autowired
    private IOrderInfoService orderInfoService;
    @JmsListener(destination = "order",containerFactory = "jmsListenerContainerQueue")
    public void receiveMsg(Message message) throws JMSException {
        if(message instanceof MapMessage){
            MapMessage mapMessage = (MapMessage) message;
            String token = mapMessage.getString("token");
            String generateOrderVoStr = mapMessage.getString("generateOrderVo");
            GenerateOrderVo generateOrderVo = JSONObject.parseObject(generateOrderVoStr,GenerateOrderVo.class);
            orderInfoService.generateOrder(token,generateOrderVo);
        }
    }
}
